package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.Commodity;
import com.yc.supermarket.entity.Sales;
import com.yc.supermarket.entity.ShoppingCart;
import com.yc.supermarket.entity.Transaction;
import com.yc.supermarket.mapper.GoodsInfoMapper;
import com.yc.supermarket.mapper.TransactionMapper;
import com.yc.supermarket.service.ITransaction;
import com.yc.supermarket.service.impl.TransactionImpl;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/tran")
public class TransactionController extends BaseController {

    private ITransaction service = new TransactionImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        System.out.println("the op:");
        if ("add".equals(op)) {
            doAdd(req, resp);
        }else if("addsale".equals(op)){
            doAddSale(req,resp);
        } else if("downCard".equals(op)){
            doDownCard(req,resp);
        }else {
            resp.getWriter().print("weige:no such Method......");
        }
    }


    protected void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Transaction transaction = parseRequest(req, Transaction.class);
        int i = service.add(transaction);
        System.out.println(transaction);
        int id = transaction.getTransactionId();
        if (i > 0) {
            printToJson(resp, id);
        } else {
            printToJson(resp, 0);
        }
    }


    protected void doAddSale(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*System.out.println("进来这个方法了吗？");*/
        String commodityName = req.getParameter("commodityName");
        GoodsInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        Commodity comByName = mapper.getComByName(commodityName);
        int commodityId = comByName.getCommodityId();
       /* System.out.println(commodityId+"pppppppppppppppppppppppppppppppp");*/

        Sales sales=parseRequest(req,Sales.class);
        BigDecimal saleAmount = sales.getSaleAmount();
        Integer transactionId = sales.getTransactionId();
        String saleDate = sales.getSaleDate();
        Integer saleQuantity = sales.getSaleQuantity();
        Integer userId = sales.getUserId();
        sales.setCommodityId(commodityId);
        /*System.out.println(sales+"wwwwwwwwwwwwwwwwwwwwww");*/
        int j=service.addSales(sales);
        printToJson(resp,j);
    }

    protected void doDownCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commodityName = req.getParameter("commodityName");
        GoodsInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(GoodsInfoMapper.class);
        Commodity comByName = mapper.getComByName(commodityName);
        int productId = comByName.getCommodityId();
        ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
        shoppingCart.setProductId(productId);
        int i = service.updateCart(shoppingCart);
        TransactionMapper mapper1;
        mapper1 = session.getMapper(TransactionMapper.class);
        mapper1.deleteCartItem();
        printToJson(resp,i);

    }
}















